<html>
<title>涨停板</title>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
<script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>
<script src="{{ url_for('static',filename='js/show_message.js') }}"></script>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<script src="{{ url_for('static',filename='js/echarts.min.js') }}"></script>

<body>
    <div>
        <label for="meeting">选择日期：</label>
        <input id="meeting" type="date" value="2022-01-03" onchange="upperCase(this.id)" />
    </div>
    <div class="table-one" style="float:left;margin-right:10px;" align="center">
        <table id="mytable" cellspacing="0"></table>
    </div>
    <div class="table-one" style="float:left;" align="center">
        <table id="broke" cellspacing="0"></table>
    </div>
    <div id="container" style="height:400px;width:30%;float:left;margin-top:60px"></div>
</body>

<!-- 日期事件 -->
<script type="text/javascript">
    function upperCase(x) {
        // 选择日期后停止运行刷新
        if (ID_THREAD != -1) {
            clearInterval(ID_THREAD)
        }

        var date = document.getElementById(x).value
        get_history_limit(date)
        get_history_broke(date)
    }
</script>
<script type="text/javascript">
    var ID_THREAD = -1; //循环线程
    var all_theme = []; //题材数组

    $(document).ready(function() {
        set_date()
        get_now_limit()
        get_broke()
        auto_update()
    });

    function auto_update() {
        data = get_server_data();
        if (get_date() == data.day) {
            ID_THREAD = setInterval('is_running()', 1000 * 3); //指定3秒刷新一次
        } else {
            if (ID_THREAD != -1) {
                clearInterval(ID_THREAD)
            }
        }
    }

    function get_server_data() {
        var m_data = []
        $.ajax({
            url: '/test',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    // 获取历史涨停 
    function get_history_limit(date) {
        var m_data = []
        $.ajax({
            url: '/history',
            data: {
                'date': date
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
                set_table(data['list'])
                set_option()
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

        // 最近几个交易日日期 
        function history_date(date) {
        var m_data = []
        $.ajax({
            url: '/history_date',
            data: {'date':date},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    // 获取历史炸板 
    function get_history_broke(date) {
        var m_data = []
        $.ajax({
            url: '/get_history_broke',
            data: {
                'date': date
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
                set_table_broke(data['list'])
                set_option()
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };


    function get_date() {
        //获取当前日期
        var d = new Date();
        var m_date = d.getFullYear() + "-" + formatZero(String(parseInt(d.getMonth()) + 1)) + "-" + formatZero(d.getDate());
        return m_date;
    }

    //格式化日期时间
    function formatZero(n) {
        if (n >= 0 && n <= 9) {
            return "0" + n;
        } else {
            return n;
        }
    }

    // 设置日期
    function set_date() {
        date = document.getElementById("meeting"); //获取表格的dom节点
        data = get_last_date()['date']
        date.value = data
    }

    // 获取上个交易日日期
    function get_last_date() {
        var m_data = []
        $.ajax({
            url: '/last_date',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            // contentType: 'application/json; charset=UTF-8',
            success: function(data) {
                m_data = data;
                console.log(data);
                // update_table(data)
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function get_time() {
        //获取当前时间
        var d = new Date();
        var m_date = d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
        // return m_date;
        // 判断时间是否在开盘时间段
        if ((15 * 60 * 60) > (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) && (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) > (9 * 60 * 60 + 15 * 60)) {
            return true
        } else {
            return false
        }
    }

    function is_running() {
        if (get_time()) {
            ret = get_now_limit()
            get_broke()
                // console.log(ret)
        } else {
            if (ID_THREAD != -1) {
                clearInterval(ID_THREAD)
            }
        }
    }

    // 获取涨停板数据
    function get_now_limit() {
        var m_data = []
        $.ajax({
            url: '/get_now_limit',
            data: {},
            type: 'POST',
            // async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data
                set_table(data['date']['list'])
                set_option()
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    // 获取炸板数据
    function get_broke() {
        var m_data = []
        $.ajax({
            url: '/limit_broke',
            data: {},
            type: 'POST',
            // async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data
                set_table_broke(data['data']['list'])
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function set_table(data) {
        all_theme = [];
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票名称</th>\
            <th>涨停时间</th>\
            <th>状态</th>\
            <th>涨停原因</th>\
            <th>板块</th>\
            <th>最后涨停时间</th>\
            <th>最大封单</th>\
            <th>封单</th>\
            <th>主力净额</th>\
            <th>成交额</th>\
            <th>实际换手</th>\
            <th>实际流通</th>\
            </tr>"
        var m_list = data
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            // 设置颜色(是否回封)
            if (m_list[i_row][24] == 1) {
                m_html += "<td class=\"gbnews\">" + m_list[i_row][1] + "</td>"
            } else
                m_html += "<td>" + m_list[i_row][1] + "</td>"
            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][6]) * 1000)) + "</td>"
            if (m_list[i_row][9] != "首板") {
                m_html += "<td class=\"red\">" + m_list[i_row][9] + "</td>"
            } else
                m_html += "<td>" + m_list[i_row][9] + "</td>"
            m_html += "<td>" + m_list[i_row][16] + "</td>"
                //数据统计
            all_theme = all_theme.concat({
                name: m_list[i_row][16],
                value: 1
            })
            m_html += "<td>" + m_list[i_row][11] + "</td>"
            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][25]) * 1000)) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][23]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][8]) + "</td>"
                // 设置颜色
            if (m_list[i_row][12] > 0) {
                m_html += "<td class=\"red\">" + format_money(m_list[i_row][12]) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + format_money(m_list[i_row][12]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][13]) + "</td>"
            m_html += "<td>" + m_list[i_row][14] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][15]) + "</td>"

            m_html += "</tr>"
        }
        var caption = "<caption align=\"top\">涨停-" + m_list.length + "</caption>"
        var tb = document.getElementById("mytable"); //获取表格的dom节点
        tb.innerHTML = caption + m_th + m_html
    }

    function set_table_broke(data) {
        all_theme = [];
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票名称</th>\
            <th>涨幅</th>\
            <th>板块</th>\
            <th>成交额</th>\
            </tr>"
        var m_list = data
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            m_html += "<td>" + m_list[i_row][1] + "</td>"
                // 设置颜色
            if (m_list[i_row][4] > 0) {
                m_html += "<td class=\"red\">" + m_list[i_row][4] + "</td>"
            } else
                m_html += "<td class=\"gre\">" + m_list[i_row][4] + "</td>"
            m_html += "<td>" + m_list[i_row][11] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][13]) + "</td>"
            m_html += "</tr>"
        }
        var tb = document.getElementById("broke"); //获取表格的dom节点
        var caption = "<caption align=\"top\">炸板-" + m_list.length + "</caption>"
        tb.innerHTML = caption + m_th + m_html
    }

    function format_money(money) {
        //金钱转换
        if (money > 0) {
            if (money / 10000 > 10000) {
                return (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return (money / 10000).toFixed(0) + "万"
            }
        } else {
            money = Math.abs(money);
            if (money / 10000 > 10000) {
                return "-" + (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return "-" + (money / 10000).toFixed(0) + "万"
            }
        }
    }

    function format_date(data) {
        //格式化日期 (data=时间戳)
        new Date(parseInt(data) * 1000)
        var h = addZero(data.getHours());
        var m = addZero(data.getMinutes());
        var s = addZero(data.getSeconds());
        return h + ":" + m + ":" + s;

        function addZero(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
    }
</script>

<script type="text/javascript">
    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    var app = {};

    var option;

    function set_option() {
        option = {
            tooltip: {
                trigger: 'item'
            },
            series: [{
                name: 'Access From',
                type: 'pie',
                radius: '100%',
                data: count_all(),
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    function count_all() {
        //数据统计
        var fruitTotal = []; // 存最终数据结果

        // 数据按照水果名称进行归类
        var nameContainer = {}; // 针对键name进行归类的容器
        all_theme.forEach(item => {
            nameContainer[item.name] = nameContainer[item.name] || [];
            nameContainer[item.name].push(item);
        });

        // console.log(nameContainer); // 按照水果名称归类完成：{ apple: Array(2), banana: Array(2) }

        // 统计不同种类水果的数量
        var fruitName = Object.keys(nameContainer); // 获取水果种类：["apple", "banana"]
        fruitName.forEach(nameItem => {
            let count = 0;
            nameContainer[nameItem].forEach(item => {
                count += item.value; // 遍历每种水果中包含的条目计算总数
            });
            fruitTotal.push({
                value: count,
                name: nameItem
            });
        });

        // console.log(fruitTotal);
        return fruitTotal
    }
</script>

</html>